package com.gf.gf_data.controller;

import com.gf.gf_data.dto.BaseResult;
import com.gf.gf_data.dto.ResultMessage;
import com.gf.gf_data.service.MtChartBarKService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * K 线数据请求控制处理器
 *
 * Created by Eternal_L on 2018/1/10.
 */
@RestController
@RequestMapping("/kd")
public class KDController {

    private static final Logger log = LoggerFactory.getLogger(KDController.class);

    private final MtChartBarKService mtChartBarKService;

    @Autowired
    public KDController(MtChartBarKService mtChartBarK1Service) {
        this.mtChartBarKService = mtChartBarK1Service;
    }

    /**
     * 获取K线数据
     *
     * @param limit 请求记录数
     * @return      K线数据集合（list）
     */
//    @Access(authorities = {"admin"})
    @GetMapping(value = "/m1/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public BaseResult obtainM1KData(@RequestParam("symbol") String symbol, Integer limit) {

        log.info("invoke-------------/k_data/list?symbol=" + symbol);
        List<Map<String, Object>> result = mtChartBarKService.getLatestMtChartBarK1Info(symbol, "M1",limit);
        // 判断是否有数据
        if (result != null && result.size() > 0)
            return new BaseResult<>(true, result);

        return new BaseResult(false, ResultMessage.NO_DATA.getState(), ResultMessage.NO_DATA.getMsg());
    }

    //    @Access(authorities = {"admin"})
    @GetMapping(value = "/m5/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public BaseResult obtainM5KData(@RequestParam("symbol") String symbol, Integer limit) {

        log.info("invoke-------------/k_data/list?symbol=" + symbol);
        List<Map<String, Object>> result = mtChartBarKService.getLatestMtChartBarK1Info(symbol, "M5", limit);
        // 判断是否有数据
        if (result != null && result.size() > 0)
            return new BaseResult<>(true, result);

        return new BaseResult(false, ResultMessage.NO_DATA.getState(), ResultMessage.NO_DATA.getMsg());
    }

    //    @Access(authorities = {"admin"})
    @GetMapping(value = "/m15/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public BaseResult obtainM15KData(@RequestParam("symbol") String symbol, Integer limit) {

        log.info("invoke-------------/k_data/list?symbol=" + symbol);
        List<Map<String, Object>> result = mtChartBarKService.getLatestMtChartBarK1Info(symbol, "M15", limit);
        // 判断是否有数据
        if (result != null && result.size() > 0)
            return new BaseResult<>(true, result);

        return new BaseResult(false, ResultMessage.NO_DATA.getState(), ResultMessage.NO_DATA.getMsg());
    }

    //    @Access(authorities = {"admin"})
    @GetMapping(value = "/m30/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public BaseResult obtainM30KData(@RequestParam("symbol") String symbol, Integer limit) {

        log.info("invoke-------------/k_data/list?symbol=" + symbol);
        List<Map<String, Object>> result = mtChartBarKService.getLatestMtChartBarK1Info(symbol, "M30", limit);
        // 判断是否有数据
        if (result != null && result.size() > 0)
            return new BaseResult<>(true, result);

        return new BaseResult(false, ResultMessage.NO_DATA.getState(), ResultMessage.NO_DATA.getMsg());
    }

    //    @Access(authorities = {"admin"})
    @GetMapping(value = "/h1/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public BaseResult obtainH1KData(@RequestParam("symbol") String symbol, Integer limit) {

        log.info("invoke-------------/k_data/list?symbol=" + symbol);
        List<Map<String, Object>> result = mtChartBarKService.getLatestMtChartBarK1Info(symbol, "H1", limit);
        // 判断是否有数据
        if (result != null && result.size() > 0)
            return new BaseResult<>(true, result);

        return new BaseResult(false, ResultMessage.NO_DATA.getState(), ResultMessage.NO_DATA.getMsg());
    }

    //    @Access(authorities = {"admin"})
    @GetMapping(value = "/h4/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public BaseResult obtainH4KData(@RequestParam("symbol") String symbol, Integer limit) {

        log.info("invoke-------------/k_data/list?symbol=" + symbol);
        List<Map<String, Object>> result = mtChartBarKService.getLatestMtChartBarK1Info(symbol, "H4", limit);
        // 判断是否有数据
        if (result != null && result.size() > 0)
            return new BaseResult<>(true, result);

        return new BaseResult(false, ResultMessage.NO_DATA.getState(), ResultMessage.NO_DATA.getMsg());
    }


    //    @Access(authorities = {"admin"})
    @GetMapping(value = "/d1/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public BaseResult obtainD1KData(@RequestParam("symbol") String symbol, Integer limit) {

        log.info("invoke-------------/k_data/list?symbol=" + symbol);
        List<Map<String, Object>> result = mtChartBarKService.getLatestMtChartBarK1Info(symbol, "D1", limit);
        // 判断是否有数据
        if (result != null && result.size() > 0)
            return new BaseResult<>(true, result);

        return new BaseResult(false, ResultMessage.NO_DATA.getState(), ResultMessage.NO_DATA.getMsg());
    }


}
